package com.hamaluik.TradeIn;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.config.Configuration;

public class TradeInItemHandler {
	private TradeIn plugin;
	public Map<String, Integer> itemNames = new HashMap<String, Integer>();
	public Map<String, Short> valueNames = new HashMap<String, Short>();
	
	// grab the main plugin so we can use it later
	public TradeInItemHandler(TradeIn instance) {
		plugin = instance;
	}
	
	public void load() {		
		// open and load the config file
		File configFile = new File(plugin.getDataFolder() + "/items.yml");
		Configuration config = new Configuration(configFile);
		config.load();

		// now get all the item names..
		List<String> itemNamesList = config.getKeys("items");
		// and build the items hashmap from these keys
		Iterator<String> it = itemNamesList.iterator();
		while(it.hasNext()) {
			String key = it.next();
			itemNames.put(key, config.getInt("items." + key, 0));
		}

		// and the value names..
		List<String> valueNamesList = config.getKeys("values");
		// and build the items hashmap from these keys
		it = valueNamesList.iterator();
		while(it.hasNext()) {
			String key = it.next();
			valueNames.put(key, (short)(int)config.getInt("values." + key, 0));
		}
	}
	
	public Integer lookupItem(String item) {
		// deal with defaults
		if(item == null || item.equals("")) {
			return null;
		}
		
		// first try to see if it's an ID number
		Integer itemId;
		try {
			itemId = Integer.parseInt(item);
		} catch(Exception e) {
			// hokay, so not an ID#. Look it up in the name list!
			if(!itemNames.containsKey(item)) {
				// ok, so we couldn't look it up..
				// this item is BOGUS!
				// let em know!
				plugin.log.info("[TradeIn] Invalid item id/name ("+item+")!");
				return null;
			}
			else {
				itemId = itemNames.get(item);
			}
		}
		return itemId;
	}
	
	public Short lookupValue(String value) {
		// deal with defaults
		if(value == null || value.equals("")) {
			return 0;
		}
		
		// deal with '*'
		if(value.equals("*")) {
			return -1;
		}
		
		// first try to see if it's an ID number
		Short valueId;
		try {
			// it was!
			valueId = Short.parseShort(value);
		} catch(Exception e) {
			// hokay, so not an ID#. Look it up in the name list!
			if(!valueNames.containsKey(value)) {
				// ok, so we couldn't look it up..
				// this item is BOGUS!
				// let em know!
				plugin.log.info("[TradeIn] Invalid value id/name ("+value+")!");
				return 0;
			}
			else {
				valueId = valueNames.get(value);
			}
		}
		return valueId;
	}
	
	public Integer countItems(Player player, ItemStack stack) {
		// grab the inventory and relevant stacks
		PlayerInventory inventory = player.getInventory();
		@SuppressWarnings("unchecked")
		HashMap<Integer, ItemStack> typeStacks = (HashMap<Integer, ItemStack>) inventory.all(stack.getTypeId());
		
		// now go through the item stacks and count!
		Integer count = 0;
		for(Integer key: typeStacks.keySet()) {
			// only collect stacks with the proper value
			if(stack.getDurability() >= 0) {
				if(stack.getDurability() != typeStacks.get(key).getDurability()) {
					// skip along, we don't have the proper value
					continue;
				}
			}
			count = count + typeStacks.get(key).getAmount();
		}
		return count;
	}

	public void giveItem(Player player, String item, Integer amount, String value) {		
		int itemId = this.lookupItem(item);
		if(amount == null) {
			amount = 64;
		}
		int itemValue = this.lookupValue(value);
	
		// grab the player's inventory so we can give em shit
		PlayerInventory inventory = player.getInventory();
		
		// create an itemstack of the item
		// with the values and whatnot
		ItemStack itemStack = new ItemStack(itemId, amount, (short)itemValue);
		
		// grab the name of the item from the game
		String itemName = itemStack.getType().name().toLowerCase().replace('_', ' ');
		
		// send them the message
		player.sendMessage(plugin.processColours("&eEnjoy your new &7"+itemName+"&e!"));
		
		// and add the item(s) to their inventory!
		inventory.addItem(itemStack);
	}
	
	public String getItemName(ItemStack stack) {
		if(stack.getTypeId() == 263) {
			// coal
			if(stack.getDurability() == 1) {
				return "coal";
			}
			else {
				return "charcoal";
			}
		}
		else if(stack.getTypeId() == 35) {
			// wool
			if(stack.getDurability() == -1) {
				return "wool";
			}
			else if(stack.getDurability() == 0) {
				return "white wool";
			}
			else if(stack.getDurability() == 1) {
				return "orange wool";
			}
			else if(stack.getDurability() == 2) {
				return "magenta wool";
			}
			else if(stack.getDurability() == 3) {
				return "light blue wool";
			}
			else if(stack.getDurability() == 4) {
				return "yellow wool";
			}
			else if(stack.getDurability() == 5) {
				return "light green wool";
			}
			else if(stack.getDurability() == 6) {
				return "pink wool";
			}
			else if(stack.getDurability() == 7) {
				return "grey wool";
			}
			else if(stack.getDurability() == 8) {
				return "light grey wool";
			}
			else if(stack.getDurability() == 9) {
				return "cyan wool";
			}
			else if(stack.getDurability() == 10) {
				return "purple wool";
			}
			else if(stack.getDurability() == 11) {
				return "blue wool";
			}
			else if(stack.getDurability() == 12) {
				return "brown wool";
			}
			else if(stack.getDurability() == 13) {
				return "dark green wool";
			}
			else if(stack.getDurability() == 14) {
				return "red wool";
			}
			else if(stack.getDurability() == 15) {
				return "black wool";
			}
		}
		else if(stack.getTypeId() == 351) {
			// wool
			if(stack.getDurability() == -1) {
				return "dye";
			}
			else if(stack.getDurability() == 0) {
				return "ink sac";
			}
			else if(stack.getDurability() == 1) {
				return "rose red";
			}
			else if(stack.getDurability() == 2) {
				return "cactus green";
			}
			else if(stack.getDurability() == 3) {
				return "cocoa beans";
			}
			else if(stack.getDurability() == 4) {
				return "lapis lazuli";
			}
			else if(stack.getDurability() == 5) {
				return "purple dye";
			}
			else if(stack.getDurability() == 6) {
				return "cyan dye";
			}
			else if(stack.getDurability() == 7) {
				return "light grey dye";
			}
			else if(stack.getDurability() == 8) {
				return "grey dye";
			}
			else if(stack.getDurability() == 9) {
				return "pink dye";
			}
			else if(stack.getDurability() == 10) {
				return "lime dye";
			}
			else if(stack.getDurability() == 11) {
				return "dandelion yellow";
			}
			else if(stack.getDurability() == 12) {
				return "light blue dye";
			}
			else if(stack.getDurability() == 13) {
				return "magenta dye";
			}
			else if(stack.getDurability() == 14) {
				return "orange dye";
			}
			else if(stack.getDurability() == 15) {
				return "bone meal";
			}
		}
		else if(stack.getTypeId() == 43 || stack.getTypeId() == 44) {
			// wool
			if(stack.getDurability() == -1) {
				return "slab";
			}
			else if(stack.getDurability() == 0) {
				return "stone slab";
			}
			else if(stack.getDurability() == 1) {
				return "sandstone slab";
			}
			else if(stack.getDurability() == 2) {
				return "wooden slab";
			}
			else if(stack.getDurability() == 3) {
				return "cobblestone slab";
			}
		}
		
		return stack.getType().name().toLowerCase().replace('_', ' ');
	}
}
